package com.yyt.module_main.act;

import android.Manifest;
import android.graphics.Color;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.vondear.rxtool.RxAppTool;
import com.yyt.libsrc.bean.AccountOverviewBean;
import com.yyt.libsrc.bean.BasicDocumentBean;
import com.yyt.libsrc.conf.Consts;
import com.yyt.libsrc.helper.LocalHelper;
import com.yyt.libsrc.helper.UpdateHelper;
import com.yyt.libsrc.net.JavaParamsBuilder;
import com.yyt.libsrc.net.NetUtil;
import com.yyt.libsrc.net.SuccessCallback;
import com.yyt.libsrc.router.RouteTable;
import com.yyt.libsrc.router.RouterHelper;
import com.yyt.module_main.R;
import com.yyt.module_main.databinding.ActivityIndexBinding;
import com.yyt.module_main.home.MainActivity;
import com.yyt.module_main.vm.IndexVM;

import io.reactivex.functions.Consumer;
import top.lixb.libcommon.base.RefreshableActivity;
import top.lixb.libcommon.utils.StringUtils;

@Route(path = RouteTable.main_index)
public class IndexActivity extends RefreshableActivity<ActivityIndexBinding> {

    private IndexVM mVm;
    public String[] mPermissions = {Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    @Override
    protected void init() {
        mVm = new IndexVM();
        mRefreshContentBinding.setVm(mVm);
        setTitle(RxAppTool.getAppName(this));
        showToolbarDivider();
        setStatusbarBackgroundColor(getResources().getColor(R.color.color_status_bar));
        mIvRight.setImageResource(R.mipmap.icon_setting);
        mIvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RouterHelper.getInstance().startAct(RouteTable.main_sys_setting);
            }
        });
        mIvLeft.setVisibility(View.GONE);

        final RxPermissions permissions = new RxPermissions(this);
        permissions.request(mPermissions).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if (!aBoolean) {
                    permissions.request(mPermissions);
                }
            }
        });

        checkUpdate();
    }

    private void checkUpdate() {
        new UpdateHelper().checkUpdate(this);
    }

    @Override
    protected int initRefreshLayout() {
        return R.layout.activity_index;
    }

    @Override
    protected void getData(boolean refresh) {
        showLoading();
        // TODO: 2018/8/8 获取商家基本信息
        NetUtil.sellerPost(new JavaParamsBuilder()
                .addCmd(Consts.CMD.basicDocument)
                .build(), new SuccessCallback<BasicDocumentBean>() {
            @Override
            public void onSuccess(BasicDocumentBean basicDocumentBean) {
                LocalHelper.saveSid(String.valueOf(basicDocumentBean.data.sid));
                LocalHelper.saveHeadIconUrl(basicDocumentBean.data.headImg);
                LocalHelper.saveUserName(basicDocumentBean.data.name);
                LocalHelper.saveMyrole(basicDocumentBean.data.role);
                LocalHelper.saveMyrate(String.valueOf(basicDocumentBean.data.rate));
            }

            @Override
            public void onComplete() {
                super.onComplete();
                getAccountInfo();
            }
        });
    }

    private void getAccountInfo() {
        NetUtil.sellerPost(new JavaParamsBuilder()
                .addCmd(Consts.CMD.accountOverview)
                .build(), new SuccessCallback<AccountOverviewBean>() {
            @Override
            public void onSuccess(AccountOverviewBean accountOverviewBean) {
                double v = Double.valueOf(accountOverviewBean.data.recommendedBalance) + Double.valueOf(accountOverviewBean.data.bonusBalance);
                mVm.incomeAmount.set(StringUtils.doubleFormat(String.valueOf(v)));
            }

            @Override
            public void onComplete() {
                super.onComplete();
                hideLoading();
                finishGetData();
            }
        });
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        getData(true);
    }
}
